package com.brand.prisd.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.brand.prisd.R;
import com.brand.prisd.sp.MySharedPreferences;
import com.orhanobut.logger.Logger;
import com.tuya.smart.android.device.utils.WiFiUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WiFiActivity extends AppCompatActivity {

    @BindView(R.id.tv_ChangeTheNetWork)
    TextView tvChangeTheNetWork;
    @BindView(R.id.et_wifi_psw)
    EditText etWifiPsw;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.tv_WiFiName)
    TextView tvWiFiName;
    private String wifiKey = "wifiKey";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wi_fi);
        ButterKnife.bind(this);
        initView();
    }


    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        setTitle("添加设备");
        String currentSSID = WiFiUtil.getCurrentSSID(this);
        tvWiFiName.setText(currentSSID);
        String string = MySharedPreferences.Build(this).getString(wifiKey);
        if (string != null) {
            etWifiPsw.setText(string);
        }
    }


    private void go2ConnectActivity() {
        String string = etWifiPsw.getText().toString();

        boolean empty = TextUtils.isEmpty(string);
        if (empty) {
            Toast.makeText(this, "密码为空！", Toast.LENGTH_SHORT).show();
        } else {
            Boolean aBoolean = MySharedPreferences.Build(this).putString(wifiKey, string);
            if (!aBoolean) {
                Logger.e("存放密码失败！");
            } else {
                Logger.i("保存密码成功！");
            }
            Intent intent = new Intent(this, ConnectActivity.class);
            intent.putExtra("WifiPsw", string);
            startActivity(intent);
            finish();
        }
    }


    @OnClick({R.id.tv_ChangeTheNetWork, R.id.btn_confirm, R.id.iv_right_arrow})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_ChangeTheNetWork:
                go2SystemWifi();
//                Toast toast = Toast.makeText(this, "待开发...", Toast.LENGTH_SHORT);
//                toast.setGravity(Gravity.CENTER, 120, 60);
//                toast.show();
                break;
            case R.id.btn_confirm:
                go2ConnectActivity();
                break;
            case R.id.iv_right_arrow:
                finish();
                break;
        }
    }

//    private void go2AllDevicesActivity() {
//        startActivity(new Intent(this, AllDevicesActivity.class));
//        finish();
//    }

    private void go2SystemWifi() {
        Intent intent = new Intent();
        intent.setClassName("com.android.settings", "com.android.settings.Settings$WifiSettingsActivity");
        startActivity(intent);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Logger.d("onRestoreInstanceState");
    }
}
